package com.example.event_resource_java.trainapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.event_resource_java.authority.dao.StatisticDao.CoursesDO;
import com.example.event_resource_java.trainapi.dao.JzCertificateProfileDo;
import com.example.event_resource_java.trainapi.dao.TrainingResourcesDO;
import com.example.event_resource_java.trainapi.dao.vo.ChapterVo;
import com.example.event_resource_java.trainapi.dao.vo.JzCertificateProfileVo;
import com.example.event_resource_java.trainapi.dao.vo.TrainingResourcesVo;
import com.example.event_resource_java.trainapi.dao.JzChapterDo;
import com.example.event_resource_java.trainapi.mapper.TrainingResourcesMapper;
import com.example.event_resource_java.trainapi.service.CoursesService;
import com.example.event_resource_java.trainapi.service.JzCertificateProfileService;
import com.example.event_resource_java.trainapi.service.JzChapterService;
import com.example.event_resource_java.trainapi.service.TrainingResourcesService;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TrainingResourcesServiceImpl extends ServiceImpl<TrainingResourcesMapper, TrainingResourcesDO> implements TrainingResourcesService {
    @Autowired
    private CoursesService coursesService;
    @Autowired
    private JzChapterService jzChapterService;
    @Autowired
    private JzCertificateProfileService jzCertificateProfileService;
//    @Override
//    public PageVo<CoursesVo> getTrains(String coursesName, String level, Integer pageNum, Integer pageSize) {
//        QueryWrapper<TrainingResourcesDO> wrapper = new QueryWrapper<TrainingResourcesDO>().orderByAsc("id");
//        List<TrainingResourcesVo> list=new ArrayList<>();
//        String strr = course(coursesName,"");
//        if (StringUtils.isNotBlank(level)){
//            level="（"+level+"）";
//            strr = course(coursesName,level);
//
//        }
//        if (StringUtils.isBlank(strr)) {
//            wrapper.eq("courses_id", null);
//        } else if(!strr.equals("@#$%^&*!~")){
//            wrapper.in("courses_id", strr.substring(0, strr.length() - 1));
//        }
//        PageVo<TrainingResourcesVo> voRes = new PageVo<>();
//        PageHelper.startPage(pageNum, pageSize, "id");
//        List<TrainingResourcesDO> trainingResourcesDOS = this.list(wrapper);
//        PageInfo<TrainingResourcesDO> page = new PageInfo<TrainingResourcesDO>(trainingResourcesDOS);
//        voRes.setTotal((int) page.getTotal());
//        voRes.setPageTotal(page.getPages());
//        voRes.setPageNum(page.getPageNum());
//        voRes.setPageSize(page.getPageSize());
//        for (TrainingResourcesDO trainingResourcesDO:trainingResourcesDOS) {
//            TrainingResourcesVo vo = new TrainingResourcesVo();
//            BeanUtils.copyProperties(trainingResourcesDO, vo);
//            list.add(vo);
//        }
//        voRes.setList(list);
//        return voRes;
//    }voRes

    @Override
    public TrainingResourcesVo getTrainsById(Long coursesId) {
        TrainingResourcesVo vo=new TrainingResourcesVo();
        TrainingResourcesDO trainingResourcesDO = this.getById(coursesId);
        BeanUtils.copyProperties(trainingResourcesDO,vo);
        List<ChapterVo> voList=new ArrayList<>();
        List<JzChapterDo> list = jzChapterService.list(new QueryWrapper<JzChapterDo>().eq("courses_id", coursesId));
        for (JzChapterDo jzChapterDo:list){
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(jzChapterDo,chapterVo);
            voList.add(chapterVo);
        }
        List<JzCertificateProfileVo> jzCertificateProfileVoList=new ArrayList<>();
        List<JzCertificateProfileDo> jzCertificateProfileDos = jzCertificateProfileService.list(new QueryWrapper<JzCertificateProfileDo>().eq("id", trainingResourcesDO));
        for (JzCertificateProfileDo jzCertificateProfileDo:jzCertificateProfileDos){
            JzCertificateProfileVo vo1=new JzCertificateProfileVo();
            BeanUtils.copyProperties(jzCertificateProfileDo,vo1);
            jzCertificateProfileVoList.add(vo1);
        }
        return vo;
    }

    private String course(String coursesName,String level) {
        String straa = "";
        if (StringUtils.isNotBlank(coursesName)) {
            QueryWrapper<CoursesDO> Wrapper = new QueryWrapper<CoursesDO>().like("name", coursesName+level);
            List<CoursesDO> coursesDOS = coursesService.list(Wrapper);
            for (CoursesDO coursesDO : coursesDOS) {
                straa = straa + coursesDO.getId() + ",";
            }

        } else {
            straa = "@#$%^&*!~";
        }
        return straa;
    }
}
